package com.dzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dzy.entity.Admin;
import com.dzy.entity.Permission;
import com.dzy.service.AdminService;
import com.dzy.service.PermissionService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
//@RequestMapping
public class FrameController {
    @Reference
    private PermissionService permissionService;
    @Reference
    private AdminService adminService;
    @RequestMapping
    public String toIndex(Model model){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        Long adminId=1L;
        User principal = (User) authentication.getPrincipal();
        String username = principal.getUsername();
        Admin admin = this.adminService.getByUsername(username);
        Long adminId=admin.getId();
        List<Permission> permissionList = this.permissionService.findMenuPermissionByAdminId(adminId);
        model.addAttribute("permissionList",permissionList);
        //Admin admin = this.adminService.getById(adminId);
        model.addAttribute("admin",admin);
        return "frame/index";
    }
    @RequestMapping("/main")
    public String main(){
        return "frame/main";
    }
    @RequestMapping("/loginPage")
    public String loginPage(){
        return "frame/login";
    }

    @GetMapping("/auth")
    public String auth() {
        return "frame/auth";
    }
}
